package com.controller;

import com.constant.Constant;
import com.entity.GoldPoints;
import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.service.GoldPointsService;
import com.service.UserService;
import com.util.AjaxResult;
import com.vo.GoldPointsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
@RequestMapping("/gp")
public class GoldPointsController {
    @Autowired
    GoldPointsService goldPointsService;

    @Autowired
    UserService userService;

    //访问积分金币记录页面将该用户的积分金币信息对象存入session作用域
    @RequestMapping("/front_record.do")
    public String toRecordPage(HttpServletRequest request){
        System.out.println("=======================");

        User user =(User) request.getSession().getAttribute(Constant.SESSION_USER);
        GoldPointsVo goldPoint = goldPointsService.findGoldPointsVoByUid(user.getId());
        System.out.println(goldPoint);
        request.getSession().setAttribute("userGpInfo",goldPoint);
        return "/front/front_record";
    }

    //分页显示积分记录
    @RequestMapping("/findRecord.do")
    @ResponseBody
    public AjaxResult findRecord(@RequestParam("pageNo")Integer pageNo,HttpServletRequest request) {
        AjaxResult result = new AjaxResult(true, "查询成功", null);
//        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
        System.out.println(pageNo);
        User user =(User)request.getSession().getAttribute(Constant.SESSION_USER);
        PageInfo<GoldPoints> userRecord = goldPointsService.findUserRecord(user,pageNo);
        result.setObj(userRecord);
        return result;
    }

    //前台显示用户剩余金币和积分
    @RequestMapping("/findFrontSumByUid.do")
    @ResponseBody
    public AjaxResult findFrontSumByUid(HttpServletRequest request){
        User user =(User) request.getSession().getAttribute(Constant.SESSION_USER);
        GoldPointsVo goldPointsVoByUid = goldPointsService.findGoldPointsVoByUid(user.getId());
        AjaxResult ajaxResult = new AjaxResult(true, "查询成功", null);
        ajaxResult.setObj(goldPointsVoByUid);
        return ajaxResult;
    }


    //前台兑换金币
    @PostMapping("/exchangePoints")
    public String pointsToGold(String pointscount,HttpServletRequest request){
        Integer count = Integer.valueOf(pointscount);
        GoldPointsVo gpinfo =(GoldPointsVo) request.getSession().getAttribute("userGpInfo");
        if(gpinfo.getSum_point_count()<=0||count>gpinfo.getSum_point_count()){
            request.getSession().setAttribute("pointsmsg"," 余额不足");
        }else{
            request.getSession().removeAttribute("pointsmsg");
            User user=(User)request.getSession().getAttribute(Constant.SESSION_USER);
            int i = goldPointsService.updatePAG(user.getId(), count);
            if(i!=2){
                request.getSession().setAttribute("pointsmsg","兑换失败");
            }
        }
        return "redirect:front_record.do";
    }
}
